package com.roc.steelend.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.roc.steelend.controller.vo.returnformat.ReturnMsg;
import com.roc.steelend.controller.vo.tiaopiao.FindTiaoPiaoReception;
import com.roc.steelend.controller.vo.tiaopiao.TiaoPiaoBuyReception;
import com.roc.steelend.mapper.BuyOrderMapper;
import com.roc.steelend.mapper.ClientMapper;
import com.roc.steelend.mapper.SuppliersMapper;
import com.roc.steelend.mapper.TiaoPiaoBuyMapper;
import com.roc.steelend.pojo.BuyOrder;
import com.roc.steelend.pojo.Client;
import com.roc.steelend.pojo.Suppliers;
import com.roc.steelend.pojo.TiaoPiaoBuy;
import com.roc.steelend.service.TiaoPiaoBuyService;
import com.roc.steelend.util.LshUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Service
@Transactional(rollbackFor = Exception.class)
public class TiaoPiaoBuyServiceImpl  implements TiaoPiaoBuyService {


    @Autowired
    private TiaoPiaoBuyMapper tiaoPiaoBuyMapper;
    @Autowired
    private BuyOrderMapper buyOrderMapper;
    @Autowired
    private SuppliersMapper suppliersMapper;


    @Override
    public ReturnMsg postTiaoPiaoBuy(TiaoPiaoBuyReception tiaoPiaoBuyReception, Integer employeeId) {
        ReturnMsg msg = new ReturnMsg();
//        生成调票数据
        TiaoPiaoBuy tiaoPiaoBuy = new TiaoPiaoBuy();
        BigInteger tiaoPiaoBuyLsh = LshUtil.getTiaoPiaoBuy();
        tiaoPiaoBuy.setTiaoPiaoBuyId(tiaoPiaoBuyLsh);
        tiaoPiaoBuy.setEmployeeId(employeeId);
        tiaoPiaoBuy.setOperateTime(new Timestamp(new Date().getTime()));
        tiaoPiaoBuy.setTiaoPiaoBuyTime(tiaoPiaoBuyReception.getTiaoPiaoTime());
        tiaoPiaoBuy.setSuppliersId(tiaoPiaoBuyReception.getSupplierId());
        if (Objects.equals(tiaoPiaoBuyReception.getRemark(), "")){
            tiaoPiaoBuyReception.setRemark(null);
        }
        tiaoPiaoBuy.setRemark(tiaoPiaoBuyReception.getRemark());
        tiaoPiaoBuy.setOriginallyInvoice(tiaoPiaoBuyReception.getOldInvoice());
        tiaoPiaoBuy.setNowInvoice(tiaoPiaoBuyReception.getNewInvoice());
        int insert = tiaoPiaoBuyMapper.insert(tiaoPiaoBuy);
        if (insert!=1){
            msg.setCode(0);
            msg.setMsg("调票操作失败，请重试");
        }

        QueryWrapper<Suppliers> suppliersQueryWrapper = new QueryWrapper<>();
        suppliersQueryWrapper.eq("supplier_id",tiaoPiaoBuyReception.getSupplierId());
        Suppliers suppliers = suppliersMapper.selectList(suppliersQueryWrapper).get(0);
        suppliers.setInvoice(tiaoPiaoBuyReception.getNewInvoice());
        suppliersMapper.updateById(suppliers);

        buyOrderMapper.updateTiaoPiaoBySuppliers(tiaoPiaoBuyReception.getSupplierId(), tiaoPiaoBuyLsh);

        msg.setMsg("调票操作成功");
        msg.setCode(1);
        return msg;
    }

    @Override
    public List<HashMap<String, Object>> findTiaoPiaoDataByBuy(FindTiaoPiaoReception findTiaoPiaoReception) {
        Integer now = findTiaoPiaoReception.getCurrentPage();
        Integer size = findTiaoPiaoReception.getPageSize();
        now=(now-1)*size;
        String duiXiang = findTiaoPiaoReception.getDuiXiang();
        if (Objects.equals(duiXiang, "")||duiXiang==null){
            duiXiang=null;
        }else {
            duiXiang="%"+duiXiang+"%";
        }

        return tiaoPiaoBuyMapper.findTiaoPiaoDataByBuy(now,size,duiXiang);

    }

    @Override
    public Integer findTiaoPiaoDataByBuyCount(FindTiaoPiaoReception findTiaoPiaoReception) {
        String duiXiang = findTiaoPiaoReception.getDuiXiang();
        if (Objects.equals(duiXiang, "")||duiXiang==null){
            duiXiang=null;
        }else {
            duiXiang="%"+duiXiang+"%";
        }

        return tiaoPiaoBuyMapper.findTiaoPiaoDataByBuyCount(duiXiang);
    }

    @Override
    public List<TiaoPiaoBuy> buyTiaoPiaoDetailSelect(BigInteger tiaoPiaoId) {

        QueryWrapper<TiaoPiaoBuy> tiaoPiaoBuyQueryWrapper = new QueryWrapper<>();
        tiaoPiaoBuyQueryWrapper.eq("tiao_piao_buy_id",tiaoPiaoId);

        return tiaoPiaoBuyMapper.selectList(tiaoPiaoBuyQueryWrapper);
    }
}
